<template>
  <el-dialog
    class="xxx"
    title="新增员工"
    :visible.sync="isShow"
    @close="closeEvent"
  >
    <el-form ref="addform" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="form.timeOfEntry"
          type:date
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment">
          <el-option
            v-for="item in employeesData.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="form.departmentName"
          placeholder="请选择"
          @focus="focusEvent"
        />
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="departmentlist"
          :props="{ label: 'name' }"
          @node-click="nodeclick"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="form.correctionTime"
          type:date
          placeholder="选择日期"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button>取消</el-button>
        <el-button type="primary" @click="addClick">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysUserPost } from '@/api/employees'
import { checkPhone } from '@/utils/validate'
import { companydepartment } from '@/api/departments.js'
import { changeData } from '@/utils/index'
import employeesData from '@/api/constant/employees.js'
export default {
  name: '',
  data() {
    return {
      mode: 'add',
      employeesData,
      isShow: false,
      departmentlist: [],
      showTree: false,
      loading: false,
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号	:'' //
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'change' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              checkPhone(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            },
            trigger: 'change'
          }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: 'change' }
        ],
        departmentName: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职日期', trigger: 'change' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'change' }
        ]
      }
    }
  },
  computed: {},
  created() {},
  methods: {
    addClick() {
      this.$refs.addform.validate(async(valid) => {
        if (valid) {
          await sysUserPost(this.form)
          this.$message.success('新增成功')
          this.$emit('getData')
          this.closeEvent()
        }
      })
    },
    async focusEvent() {
      this.showTree = true
      // 如果没有数据 就调用接口
      if (this.departmentlist.length === 0) {
        this.loding = true
        // 获取组件架构数据
        const res = await companydepartment()
        this.loading = false
        this.departmentlist = changeData(res.data.depts, '')
        // this.departmentlist = this.changeData(res.data.depts, '')
      }
    },
    nodeclick(row) {
      this.form.departmentName = row.name
      this.showTree = false
    },
    closeEvent() {
      this.$refs.addform.resetFields()
      this.isShow = false
      this.showTree = false
      this.form = {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号	:'' //
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: '' //	string	非必须		转正时间
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.xxx {
  text-align: left;
}
</style>
